#include <StopWatch.h>
#include <csp_debug.h>
#include <csp_macros.h>
#include <csp_shared_types.h>
#include <csp_protocol_msg.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include <sys/time.h>
#include <string.h>
#include <stdio.h>


using namespace std;

StopWatch::StopWatch()
{
	//sw_start=0.0;
	//sw_stop=0.0;
	//total=0.0;
	clear();
}

StopWatch::~StopWatch()
{
}

void StopWatch::clear(){
	sw_start=0.0;
	sw_stop=0.0;
	total=0.0;
}

void StopWatch::start(){
	gettimeofday(&sw_tvstruct, NULL);
	sw_start=1.0*sw_tvstruct.tv_sec+sw_tvstruct.tv_usec/1000000.0;
}

void StopWatch::stop(){
	gettimeofday(&sw_tvstruct, NULL);
	sw_stop=1.0*sw_tvstruct.tv_sec+sw_tvstruct.tv_usec/1000000.0;
	total+=sw_stop-sw_start;
}

int StopWatch::readDays(){
	return (int)((total)/(24*60*60));
}

int StopWatch::readHours(){
	return (int)((total)/(60*60))%24;
}

int StopWatch::readMinutes(){
	return (int)((total)/(60))%60;
}

int StopWatch::readSeconds(){
	return (int)((total))%60;
}

int StopWatch::readmSeconds(){
	return ((int)((total)*1000))%1000;
}


void StopWatch::timeString(char *buf) {
	buf[0]=0;

	if(readDays()>0)sprintf(buf,"%d days ",readDays()); 
	buf=strchr(buf,0);
	if(readHours()>0)sprintf(buf,"%d hours ",readHours()); 
	buf=strchr(buf,0);
	if(readMinutes()>0)sprintf(buf,"%d min. ",readMinutes()); 
	buf=strchr(buf,0);
	if(readSeconds()>0)sprintf(buf,"%d seconds ",readSeconds()); 
	buf=strchr(buf,0);
	sprintf(buf,"%d ms ",readmSeconds()); 
}

